package com.jiaai.web.zfp.callback;



import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.web.zfp.callback.entity.LiveCallbackData;
import com.jiaai.live.api.ILiveApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * <p>
 *  直播回调
 *  包含鉴权,开始推流 ,结束推流 ,开始观看,结束观看
 * </p>
 * @author 佳爱网络科技
 * @version V1.0
 * @title LiveCallbackController
 * @since 2017/7/30
 */
@RestController
@RequestMapping("/live/callback")
public class LiveCallbackController {
    public static  final Logger  log= LoggerFactory.getLogger(LiveCallbackController.class);

    @Reference
    public ILiveApi liveApi;

    /**
     * 当客户端发布流时，譬如flash/FMLE方式推流到服务器
     * @param
     * @return
     */
    @PostMapping("/onPublish")
    public  void onPublish(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");

        } catch (IOException e) {
            e.printStackTrace();
        }
        // return RestResultGenerator.genSuccessResult();
    }

    /**
     * 监听客户端结束推流事件
     * @param data
     * @param response
     */
    @PostMapping("/onUnpublish")
    public  void onUnpublish(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
            try {
                //liveApi.updateStatusByStream(LIVEStatus.STOP.getType(),data.getStream());
            } catch (Exception e) {
                log.error("更改直播间状态失败!error:"+e.getMessage());
            }
        } catch (IOException e) {
            log.error(" writer error:"+e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 监听客户端观看直播事件
     * @param data
     * @param response
     */
    @PostMapping("/onPlay")
    public  void onPlay(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 监听客户端结束观看事件
     * @param data
     * @param response
     */
    @PostMapping("/onStop")
    public  void onStop(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 监听客户端结束录播事件
     * @param data
     * @param response
     */
    @PostMapping("/onDvr")
    public  void onDvr(@RequestBody LiveCallbackData data, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().append("0");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
